<?php
/**
 * Hellos View for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view' );

/**
 * Hellos View
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class TargetsViewComments extends JView
{
	/**
	 * Hellos view display method
	 * @return void
	 **/
	 
	#function __construct() {
	#	$this->setModel('target');
	#	parent::__construct();
	#}
 
	function display($tpl = null)
	{
		$comments = $this->get('Comments');
		$count_comments = $this->get('CountComments');
		
		$per_page = $this->get('PerPage');		
		$count_pages = ceil($count_comments/$per_page);
		
		$current_page = JRequest::getVar('page', 1, 'int');
		#$count_pages = 100;
		$count_pages_vis = 7;
		
		$half_left_cnt = floor($count_pages_vis / 2);
		$half_right_cnt = $count_pages_vis - $half_left_cnt;
		$page = $current_page - 1;
		
		$vis_pages = array();
		while ($page > 0 && $half_left_cnt > 0) {
			array_unshift($vis_pages, $page);
			$page--;
			$half_left_cnt--;
		}
		$half_right_cnt += $half_left_cnt;
		
		$page = $current_page;
		for ($i = 0; $i < $half_right_cnt && $page <= $count_pages; $i++) {
			array_push($vis_pages, $page);
			$page++;
		}
		
		#print_r($count_pages);
		
		#$this->assign('count_pages', 100);
		
		$target_id = JRequest::getVar('target_id', 0, 'int');
		$prefix = JRequest::getVar('prefix', 0, 'int');
		
		$target_model = $this->getModel('target');
		$targetName = $target_model->getTargetName($target_id);
		
		$f = fopen('/tmp/test4.txt', 'w');
		fwrite($f, $targetName);
		fclose($f);
		
		
		$this->assignRef('comments', $comments);
		$this->assignRef('pages', $vis_pages);
		$this->assign('target_id', $target_id);
		$this->assign('target_name', $targetName);
		$this->assign('prefix', $prefix);
		$this->assign('current_page', $current_page);
		parent::display($tpl);
	}
}